package cc.grapevine.netcard.jspservlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cc.grapevine.netcard.tools.ApiString;
import cc.grapevine.netcard.tools.BaseAuthenicationHttpClient;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class Login extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public Login() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		out.print("<font color=ff0000>Illegal calls!!</font>");

		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		// PrintWriter out = response.getWriter();

		String mail = request.getParameter("login");
		String pwd = request.getParameter("password");

		// String returnstr = response.encodeURL("./api/RegServlet");

		if (null != mail && null != pwd) {
			String url = ApiString.LOACLHOST + "api/LoginServlet";
			String jsonString = null;
			HashMap<String, String> parms = new HashMap<String, String>();
			parms.put(ApiString.MAIL, mail);
			parms.put(ApiString.PWD, pwd);

			jsonString = BaseAuthenicationHttpClient.doPost(url, parms);

			JsonParser parser = new JsonParser();
			JsonElement element = parser.parse(jsonString);
			JsonObject jo = element.getAsJsonObject();
			int into = jo.get("error").getAsInt();
			JsonElement idElemet = jo.get("id");
			long id = -1;
			if (null != idElemet) {
				id = idElemet.getAsLong();
			}

			// int into = jo.getAsInt();
			System.out.println("=============" + into + "==================="
					+ id);
			// JSONObject jsonObject = new JSONObject(jsonString);
			if (into == 1) {
				request.setAttribute("id", id);
				response.sendRedirect("../wanglixing.jsp");
			} else {
				response.sendRedirect("../login.jsp");
			}
			System.out.println(jsonString);
		} else {
			
			response.sendRedirect("../login.jsp");
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
